package com.sky.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.sky.context.BaseContext;
import com.sky.mapper.EmployeeMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.beans.beancontext.BeanContext;
import java.time.LocalDateTime;

@Slf4j
@Component
public class AutoFillHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("insert fill start");
        Object createTime = metaObject.getValue("createTime");
        Object updateTime = metaObject.getValue("updateTime");
        Object createUser = metaObject.getValue("createUser");
        Object updateUser = metaObject.getValue("updateUser");
        if(createTime == null || createTime.equals("")) {
            metaObject.setValue("createTime", LocalDateTime.now());
        }
        if(updateTime == null || updateTime.equals("")) {
            metaObject.setValue("updateTime", LocalDateTime.now());
        }
        if(createUser == null || createUser.equals("")) {
            metaObject.setValue("createUser", BaseContext.getCurrentId());
        }
        if(updateUser == null || updateUser.equals("")) {
            metaObject.setValue("updateUser", BaseContext.getCurrentId());
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("update fill start");
        Object Id = BaseContext.getCurrentId();
//        Object createTime = employeeMapper
        Object updateTime = metaObject.getValue("updateTime");
        Object updateUser = metaObject.getValue("updateUser");
        if(updateTime == null || updateTime.equals("")) {
            metaObject.setValue("updateTime", LocalDateTime.now());
        }
        if(updateUser == null || updateUser.equals("")) {
            metaObject.setValue("updateUser", BaseContext.getCurrentId());
        }

    }

}
